<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="username" placeholder="请输入用户名" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <!--      <el-select v-model="listQuery.importance" placeholder="订单状态" clearable style="width: 120px" class="filter-item">-->
      <!--        <el-option v-for="item in importanceOptions" :key="item" :label="item" :value="item" />-->
      <!--      </el-select>-->
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>
    <el-table

      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      tablder
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column prop="index" label="序号" align="center" width="80" fixed>
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>

      <el-table-column label="店铺名称" prop="brandsName" align="center" />

      <!--      操作-->
      <el-table-column label="注册用户详细" align="center" class-name="small-padding fixed-width">
        <template slot-scope="row">
          <el-button v-if="row.status!='deleted'" size="mini" type="primary" @click="findUser(row.row.userId)">
            注册用户详细
          </el-button>
        </template>
      </el-table-column>

      <el-table-column label="店铺描述" prop="brandsText" align="center" />

      <!--      操作-->
      <el-table-column label="店铺认证信息" align="center" class-name="small-padding fixed-width">
        <template slot-scope="row">
          <el-button v-if="row.status!='deleted'" size="mini" type="primary" @click="findImg(row.row.brandsCertify)">
            查看提交
          </el-button>
        </template>
      </el-table-column>

      <el-table-column label="等级" prop="brandsLabel" align="center" />

      <el-table-column label="创建时间" prop="createTime" align="center" />

      <el-table-column label="更新时间" prop="updateTime" align="center" />
      <!--      操作-->
      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
        <template slot-scope="row">

          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDelete(row.row.brandsId)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    分页-->
    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!--    查看认证信息弹窗-->
    <el-dialog
      title="店铺认证信息"
      :visible.sync="dialogVisible"
      width="30%"
    >

      <div>
        <viewer :images="imgList">
          <img
            v-for="(src,index) in imgList"
            :key="index"
            style="width: 196px; height: 196px; border:none;cursor: pointer;"
            :src="getImage(src.imagename)"
          >
        </viewer>
      </div>

    </el-dialog>

    <!--    查看用户信息-->
    <el-dialog
      title="用户信息"
      :visible.sync="dialogVisible2"
      width="30%"
    >
      <!--弹窗内容-->
      <el-descriptions :column="1" border>
        <el-descriptions-item label="微信头像">      <!--        图片-->
          <el-image
            style="width: 100px; height: 100px; border:none;cursor: pointer;"
            :src="getWxIconImage(this.usermsg.userIcon)"
          >’
          </el-image>
        </el-descriptions-item>
        <el-descriptions-item label="用户名">{{ usermsg.userName }}</el-descriptions-item>
        <el-descriptions-item label="性别">{{ usermsg.userSex == 0 ? "男" : "女" }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ usermsg.userPhone }}</el-descriptions-item>
        <el-descriptions-item label="邮箱">{{ usermsg.userEmail }}</el-descriptions-item>

      </el-descriptions>

    </el-dialog>

  </div>
</template>

<script>

import waves from '@/directive/waves' // waves directive

import Pagination from '@/components/Pagination' // secondary package based on el-pagination

import { deleteuser, getuser } from '@/api/brand'

import { getuserByid } from '@/api/userAndDesign'
const calendarTypeOptions = [
  { key: 'CN', display_name: 'China' },
  { key: 'US', display_name: 'USA' },
  { key: 'JP', display_name: 'Japan' },
  { key: 'EU', display_name: 'Eurozone' }
]

// arr to obj, such as { CN : "China", US : "USA" }
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },

  data() {
    return {
      // 表格列表
      list: [],
      // 搜索框
      username: '',
      // 分页总数
      total: 0,
      // 当前页
      currentPage: 1,
      // 每页多少条
      pagesizes: 5,
      // 查看图片放大
      dialogVisible: false,
      // 查看用户
      dialogVisible2: false,
      // 用户信息
      usermsg: {},
      // 多图片放大列表
      imgList: [],
      // 搜索下拉列表
      // importanceOptions: [1, 2, 3],
      tableKey: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      calendarTypeOptions,
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
      statusOptions: ['published', 'draft', 'deleted'],
      showReviewer: false,
      temp: {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        type: '',
        status: 'published'
      },
      dialogFormVisible2: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      getuser(this.username, this.currentPage, this.pagesizes).then(res => {
        this.list = res.data.records
        this.total = res.data.total
        console.log(this.list)
        this.listLoading = false
      })
    },
    // 删除
    handleDelete(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteuser(id).then(res => {
          console.log(res)
          this.$notify({
            title: res.data,
            message: res.data,
            type: 'success',
            duration: res.code
          })
        })
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 图片放大
    findImg(id) {
      this.imgList = []
      console.log('这是查看图片！')
      var obj = JSON.parse(id) // JSON.parse() 把 json字符串 转化成 json 对象

      for (const i in obj) {
        this.imgList.push(obj[i])
      }

      console.log(this.imgList)

      this.dialogVisible = true
    },
    // 查看用户
    findUser(userId) {
      console.log('这是查看用户！')
      console.log(userId)

      getuserByid(userId).then(res => {
        console.log(res)
        this.usermsg = res.data
      })

      this.dialogVisible2 = true
    },
    // 搜索框
    handleFilter() {
      this.getList()
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    },
    handleSizeChange(val) {
      this.pagesizes = val
      console.log(this.pagesizes)
      this.getList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      console.log(this.currentPage)
      this.getList()
    },
    // 微信头像
    getWxIconImage(url) {
      // console.log(url)
      return `${url}`
    },
    // 得到图片
    getImage(image) {
      return `http://localhost:9527/dev-api/common/download?name=${image}`
    }
  }
}
</script>
